package com.leonwoodstudio.endorsementclient;

import android.content.Context;
import android.os.Environment;

import org.apache.http.util.EncodingUtils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * Created by win7 on 2017/3/17.
 */
public class FileAccessor {
    private static FileAccessor instance = null;
    Context context;
    static String ext_dir = Environment.getExternalStorageDirectory() + File.separator + "Endorsement" + File.separator;

    private FileAccessor(Context context) {
        this.context = context;
        if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            ((MainActivity)context).status_message.setText("外存不可用,请检查手机状态!");
        }
        File ext_dir = new File(this.ext_dir);
        if (!ext_dir.exists() && !ext_dir.mkdirs()) {
            ((MainActivity)context).status_message.setText("创建目录失败,请检查手机权限!");
        }
    }

    public static FileAccessor getInstance(Context context) {
        if (instance != null) {
            return instance;
        }
        if (context != null) {
            instance = new FileAccessor(context);
        }
        return instance;
    }

    public String getFilesDir() {
        //return context.getExternalFilesDir(null) + File.separator;
        return ext_dir;
    }

    public File getFile(String fileName) {
        File file = new File(fileName);
        if(file.exists()) {
            return file;
        }
        else {
            try {
                file.createNewFile();
                return file;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return null;
            }
        }
    }

    public boolean cleanFile(String fileName) {
        String cleanStr = "";
        try {
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(getFile(fileName)));
            bos.write(cleanStr.getBytes());
            bos.flush();
            bos.close();
            return true;
        }
        catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
    public boolean isFileExist(String fileName){
        File file = new File(fileName);
        return file.exists();
    }
    public void deleteFile(String fileName) {
        if(isFileExist(fileName)) {
            getFile(fileName).delete();
        }
    }

    public boolean writeSdFile(String fileName, byte[] content) {
        boolean ret = true;
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(fileName);
            fos.write(content);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ret = false;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            ret = false;
        } finally {
            try {
                if(fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                ret = false;
            }
        }
        return ret;
    }

    public static String readFile(String fileName) {
        String ret = null;
        FileInputStream fis = null;
        try {
            fis= new FileInputStream(fileName);
            int len = fis.available();
            byte[] buf = new byte[len];
            fis.read(buf);
            ret = EncodingUtils.getString(buf, "UTF-8");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            fis = null;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            fis = null;
        } finally {
            try {
                if(fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return ret;
    }

}
